package pe.gob.mpfn.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.component.html.HtmlPanelGrid;
import javax.faces.context.FacesContext;
import javax.faces.convert.DateTimeConverter;
import javax.faces.validator.LengthValidator;
import org.primefaces.component.autocomplete.AutoComplete;
import org.primefaces.component.calendar.Calendar;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.component.inputtextarea.InputTextarea;
import org.primefaces.component.message.Message;
import org.primefaces.context.RequestContext;
import org.primefaces.event.CloseEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.jsf.managedbean.RooJsfManagedBean;
import org.springframework.roo.addon.serializable.RooSerializable;
import pe.gob.mpfn.controller.converter.ExpedienteConverter;
import pe.gob.mpfn.controller.converter.TablaMaestraConverter;
import pe.gob.mpfn.model.DetalleDiligencia;
import pe.gob.mpfn.model.Diligencia;
import pe.gob.mpfn.model.Expediente;
import pe.gob.mpfn.model.TablaMaestra;
import pe.gob.mpfn.service.DiligenciaService;
import pe.gob.mpfn.service.ExpedienteService;
import pe.gob.mpfn.service.TablaMaestraService;

@ManagedBean(name = "diligenciaBean")
@SessionScoped
@Configurable
@RooSerializable
@RooJsfManagedBean(entity = Diligencia.class, beanName = "diligenciaBean")
public class DiligenciaBean implements Serializable  {

	@Autowired
    DiligenciaService diligenciaService;

	@Autowired
    TablaMaestraService tablaMaestraService;

	@Autowired
    ExpedienteService expedienteService;

	private String name = "Diligencias";

	private Diligencia diligencia;

	private List<Diligencia> allDiligencias;

	private boolean dataVisible = false;

	private List<String> columns;

	private HtmlPanelGrid createPanelGrid;

	private HtmlPanelGrid editPanelGrid;

	private HtmlPanelGrid viewPanelGrid;

	private boolean createDialogVisible = false;

	private List<DetalleDiligencia> selectedDetalleDiligencias;

	@PostConstruct
    public void init() {
        columns = new ArrayList<String>();
        columns.add("datFechaInicio");
        columns.add("datFechaFin");
        columns.add("strObservacion");
        columns.add("chrEstado");
    }

	public String getName() {
        return name;
    }

	public List<String> getColumns() {
        return columns;
    }

	public List<Diligencia> getAllDiligencias() {
        return allDiligencias;
    }

	public void setAllDiligencias(List<Diligencia> allDiligencias) {
        this.allDiligencias = allDiligencias;
    }

	public String findAllDiligencias() {
        allDiligencias = diligenciaService.findAllDiligencias();
        dataVisible = !allDiligencias.isEmpty();
        return null;
    }

	public boolean isDataVisible() {
        return dataVisible;
    }

	public void setDataVisible(boolean dataVisible) {
        this.dataVisible = dataVisible;
    }

	public HtmlPanelGrid getCreatePanelGrid() {
        if (createPanelGrid == null) {
            createPanelGrid = populateCreatePanel();
        }
        return createPanelGrid;
    }

	public void setCreatePanelGrid(HtmlPanelGrid createPanelGrid) {
        this.createPanelGrid = createPanelGrid;
    }

	public HtmlPanelGrid getEditPanelGrid() {
        if (editPanelGrid == null) {
            editPanelGrid = populateEditPanel();
        }
        return editPanelGrid;
    }

	public void setEditPanelGrid(HtmlPanelGrid editPanelGrid) {
        this.editPanelGrid = editPanelGrid;
    }

	public HtmlPanelGrid getViewPanelGrid() {
        if (viewPanelGrid == null) {
            viewPanelGrid = populateViewPanel();
        }
        return viewPanelGrid;
    }

	public void setViewPanelGrid(HtmlPanelGrid viewPanelGrid) {
        this.viewPanelGrid = viewPanelGrid;
    }

	public HtmlPanelGrid populateCreatePanel() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Application application = facesContext.getApplication();
        ExpressionFactory expressionFactory = application.getExpressionFactory();
        ELContext elContext = facesContext.getELContext();
        
        HtmlPanelGrid htmlPanelGrid = (HtmlPanelGrid) application.createComponent(HtmlPanelGrid.COMPONENT_TYPE);
        
        HtmlOutputText detalleDiligenciasCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        detalleDiligenciasCreateOutput.setId("detalleDiligenciasCreateOutput");
        detalleDiligenciasCreateOutput.setValue("Detalle Diligencias:   ");
        htmlPanelGrid.getChildren().add(detalleDiligenciasCreateOutput);
        
        HtmlOutputText detalleDiligenciasCreateInput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        detalleDiligenciasCreateInput.setId("detalleDiligenciasCreateInput");
        detalleDiligenciasCreateInput.setValue("This relationship is managed from the DetalleDiligencia side");
        htmlPanelGrid.getChildren().add(detalleDiligenciasCreateInput);
        
        Message detalleDiligenciasCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        detalleDiligenciasCreateInputMessage.setId("detalleDiligenciasCreateInputMessage");
        detalleDiligenciasCreateInputMessage.setFor("detalleDiligenciasCreateInput");
        detalleDiligenciasCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(detalleDiligenciasCreateInputMessage);
        
        HtmlOutputText intUbigeoDiligenciaIdCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        intUbigeoDiligenciaIdCreateOutput.setId("intUbigeoDiligenciaIdCreateOutput");
        intUbigeoDiligenciaIdCreateOutput.setValue("Int Ubigeo Diligencia Id:   ");
        htmlPanelGrid.getChildren().add(intUbigeoDiligenciaIdCreateOutput);
        
        AutoComplete intUbigeoDiligenciaIdCreateInput = (AutoComplete) application.createComponent(AutoComplete.COMPONENT_TYPE);
        intUbigeoDiligenciaIdCreateInput.setId("intUbigeoDiligenciaIdCreateInput");
        intUbigeoDiligenciaIdCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{diligenciaBean.diligencia.intUbigeoDiligenciaId}", TablaMaestra.class));
        intUbigeoDiligenciaIdCreateInput.setCompleteMethod(expressionFactory.createMethodExpression(elContext, "#{diligenciaBean.completeIntUbigeoDiligenciaId}", List.class, new Class[] { String.class }));
        intUbigeoDiligenciaIdCreateInput.setDropdown(true);
        intUbigeoDiligenciaIdCreateInput.setValueExpression("var", expressionFactory.createValueExpression(elContext, "intUbigeoDiligenciaId", String.class));
        intUbigeoDiligenciaIdCreateInput.setValueExpression("itemLabel", expressionFactory.createValueExpression(elContext, "#{intUbigeoDiligenciaId.strDescripcion} #{intUbigeoDiligenciaId.chrEstado} #{intUbigeoDiligenciaId.intTablaId}", String.class));
        intUbigeoDiligenciaIdCreateInput.setValueExpression("itemValue", expressionFactory.createValueExpression(elContext, "#{intUbigeoDiligenciaId}", TablaMaestra.class));
        intUbigeoDiligenciaIdCreateInput.setConverter(new TablaMaestraConverter());
        intUbigeoDiligenciaIdCreateInput.setRequired(false);
        htmlPanelGrid.getChildren().add(intUbigeoDiligenciaIdCreateInput);
        
        Message intUbigeoDiligenciaIdCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        intUbigeoDiligenciaIdCreateInputMessage.setId("intUbigeoDiligenciaIdCreateInputMessage");
        intUbigeoDiligenciaIdCreateInputMessage.setFor("intUbigeoDiligenciaIdCreateInput");
        intUbigeoDiligenciaIdCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(intUbigeoDiligenciaIdCreateInputMessage);
        
        HtmlOutputText strNumeroExpedienteIdCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        strNumeroExpedienteIdCreateOutput.setId("strNumeroExpedienteIdCreateOutput");
        strNumeroExpedienteIdCreateOutput.setValue("Str Numero Expediente Id:   ");
        htmlPanelGrid.getChildren().add(strNumeroExpedienteIdCreateOutput);
        
        AutoComplete strNumeroExpedienteIdCreateInput = (AutoComplete) application.createComponent(AutoComplete.COMPONENT_TYPE);
        strNumeroExpedienteIdCreateInput.setId("strNumeroExpedienteIdCreateInput");
        strNumeroExpedienteIdCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{diligenciaBean.diligencia.strNumeroExpedienteId}", Expediente.class));
        strNumeroExpedienteIdCreateInput.setCompleteMethod(expressionFactory.createMethodExpression(elContext, "#{diligenciaBean.completeStrNumeroExpedienteId}", List.class, new Class[] { String.class }));
        strNumeroExpedienteIdCreateInput.setDropdown(true);
        strNumeroExpedienteIdCreateInput.setValueExpression("var", expressionFactory.createValueExpression(elContext, "strNumeroExpedienteId", String.class));
        strNumeroExpedienteIdCreateInput.setValueExpression("itemLabel", expressionFactory.createValueExpression(elContext, "#{strNumeroExpedienteId.strLugarSuceso} #{strNumeroExpedienteId.datFechaRegistro} #{strNumeroExpedienteId.datFechaSuceso} #{strNumeroExpedienteId.strNumeroExpedienteId}", String.class));
        strNumeroExpedienteIdCreateInput.setValueExpression("itemValue", expressionFactory.createValueExpression(elContext, "#{strNumeroExpedienteId}", Expediente.class));
        strNumeroExpedienteIdCreateInput.setConverter(new ExpedienteConverter());
        strNumeroExpedienteIdCreateInput.setRequired(false);
        htmlPanelGrid.getChildren().add(strNumeroExpedienteIdCreateInput);
        
        Message strNumeroExpedienteIdCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        strNumeroExpedienteIdCreateInputMessage.setId("strNumeroExpedienteIdCreateInputMessage");
        strNumeroExpedienteIdCreateInputMessage.setFor("strNumeroExpedienteIdCreateInput");
        strNumeroExpedienteIdCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(strNumeroExpedienteIdCreateInputMessage);
        
        HtmlOutputText datFechaInicioCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        datFechaInicioCreateOutput.setId("datFechaInicioCreateOutput");
        datFechaInicioCreateOutput.setValue("Dat Fecha Inicio: * ");
        htmlPanelGrid.getChildren().add(datFechaInicioCreateOutput);
        
        Calendar datFechaInicioCreateInput = (Calendar) application.createComponent(Calendar.COMPONENT_TYPE);
        datFechaInicioCreateInput.setId("datFechaInicioCreateInput");
        datFechaInicioCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{diligenciaBean.diligencia.datFechaInicio}", Date.class));
        datFechaInicioCreateInput.setNavigator(true);
        datFechaInicioCreateInput.setEffect("slideDown");
        datFechaInicioCreateInput.setPattern("dd/MM/yyyy");
        datFechaInicioCreateInput.setRequired(true);
        htmlPanelGrid.getChildren().add(datFechaInicioCreateInput);
        
        Message datFechaInicioCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        datFechaInicioCreateInputMessage.setId("datFechaInicioCreateInputMessage");
        datFechaInicioCreateInputMessage.setFor("datFechaInicioCreateInput");
        datFechaInicioCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(datFechaInicioCreateInputMessage);
        
        HtmlOutputText datFechaFinCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        datFechaFinCreateOutput.setId("datFechaFinCreateOutput");
        datFechaFinCreateOutput.setValue("Dat Fecha Fin: * ");
        htmlPanelGrid.getChildren().add(datFechaFinCreateOutput);
        
        Calendar datFechaFinCreateInput = (Calendar) application.createComponent(Calendar.COMPONENT_TYPE);
        datFechaFinCreateInput.setId("datFechaFinCreateInput");
        datFechaFinCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{diligenciaBean.diligencia.datFechaFin}", Date.class));
        datFechaFinCreateInput.setNavigator(true);
        datFechaFinCreateInput.setEffect("slideDown");
        datFechaFinCreateInput.setPattern("dd/MM/yyyy");
        datFechaFinCreateInput.setRequired(true);
        htmlPanelGrid.getChildren().add(datFechaFinCreateInput);
        
        Message datFechaFinCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        datFechaFinCreateInputMessage.setId("datFechaFinCreateInputMessage");
        datFechaFinCreateInputMessage.setFor("datFechaFinCreateInput");
        datFechaFinCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(datFechaFinCreateInputMessage);
        
        HtmlOutputText strObservacionCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        strObservacionCreateOutput.setId("strObservacionCreateOutput");
        strObservacionCreateOutput.setValue("Str Observacion: * ");
        htmlPanelGrid.getChildren().add(strObservacionCreateOutput);
        
        InputTextarea strObservacionCreateInput = (InputTextarea) application.createComponent(InputTextarea.COMPONENT_TYPE);
        strObservacionCreateInput.setId("strObservacionCreateInput");
        strObservacionCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{diligenciaBean.diligencia.strObservacion}", String.class));
        LengthValidator strObservacionCreateInputValidator = new LengthValidator();
        strObservacionCreateInputValidator.setMaximum(200);
        strObservacionCreateInput.addValidator(strObservacionCreateInputValidator);
        strObservacionCreateInput.setRequired(true);
        htmlPanelGrid.getChildren().add(strObservacionCreateInput);
        
        Message strObservacionCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        strObservacionCreateInputMessage.setId("strObservacionCreateInputMessage");
        strObservacionCreateInputMessage.setFor("strObservacionCreateInput");
        strObservacionCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(strObservacionCreateInputMessage);
        
        HtmlOutputText chrEstadoCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        chrEstadoCreateOutput.setId("chrEstadoCreateOutput");
        chrEstadoCreateOutput.setValue("Chr Estado: * ");
        htmlPanelGrid.getChildren().add(chrEstadoCreateOutput);
        
        InputText chrEstadoCreateInput = (InputText) application.createComponent(InputText.COMPONENT_TYPE);
        chrEstadoCreateInput.setId("chrEstadoCreateInput");
        chrEstadoCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{diligenciaBean.diligencia.chrEstado}", Character.class));
        chrEstadoCreateInput.setRequired(true);
        htmlPanelGrid.getChildren().add(chrEstadoCreateInput);
        
        Message chrEstadoCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        chrEstadoCreateInputMessage.setId("chrEstadoCreateInputMessage");
        chrEstadoCreateInputMessage.setFor("chrEstadoCreateInput");
        chrEstadoCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(chrEstadoCreateInputMessage);
        
        return htmlPanelGrid;
    }

	public HtmlPanelGrid populateEditPanel() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Application application = facesContext.getApplication();
        ExpressionFactory expressionFactory = application.getExpressionFactory();
        ELContext elContext = facesContext.getELContext();
        
        HtmlPanelGrid htmlPanelGrid = (HtmlPanelGrid) application.createComponent(HtmlPanelGrid.COMPONENT_TYPE);
        
        HtmlOutputText detalleDiligenciasEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        detalleDiligenciasEditOutput.setId("detalleDiligenciasEditOutput");
        detalleDiligenciasEditOutput.setValue("Detalle Diligencias:   ");
        htmlPanelGrid.getChildren().add(detalleDiligenciasEditOutput);
        
        HtmlOutputText detalleDiligenciasEditInput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        detalleDiligenciasEditInput.setId("detalleDiligenciasEditInput");
        detalleDiligenciasEditInput.setValue("This relationship is managed from the DetalleDiligencia side");
        htmlPanelGrid.getChildren().add(detalleDiligenciasEditInput);
        
        Message detalleDiligenciasEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        detalleDiligenciasEditInputMessage.setId("detalleDiligenciasEditInputMessage");
        detalleDiligenciasEditInputMessage.setFor("detalleDiligenciasEditInput");
        detalleDiligenciasEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(detalleDiligenciasEditInputMessage);
        
        HtmlOutputText intUbigeoDiligenciaIdEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        intUbigeoDiligenciaIdEditOutput.setId("intUbigeoDiligenciaIdEditOutput");
        intUbigeoDiligenciaIdEditOutput.setValue("Int Ubigeo Diligencia Id:   ");
        htmlPanelGrid.getChildren().add(intUbigeoDiligenciaIdEditOutput);
        
        AutoComplete intUbigeoDiligenciaIdEditInput = (AutoComplete) application.createComponent(AutoComplete.COMPONENT_TYPE);
        intUbigeoDiligenciaIdEditInput.setId("intUbigeoDiligenciaIdEditInput");
        intUbigeoDiligenciaIdEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{diligenciaBean.diligencia.intUbigeoDiligenciaId}", TablaMaestra.class));
        intUbigeoDiligenciaIdEditInput.setCompleteMethod(expressionFactory.createMethodExpression(elContext, "#{diligenciaBean.completeIntUbigeoDiligenciaId}", List.class, new Class[] { String.class }));
        intUbigeoDiligenciaIdEditInput.setDropdown(true);
        intUbigeoDiligenciaIdEditInput.setValueExpression("var", expressionFactory.createValueExpression(elContext, "intUbigeoDiligenciaId", String.class));
        intUbigeoDiligenciaIdEditInput.setValueExpression("itemLabel", expressionFactory.createValueExpression(elContext, "#{intUbigeoDiligenciaId.strDescripcion} #{intUbigeoDiligenciaId.chrEstado} #{intUbigeoDiligenciaId.intTablaId}", String.class));
        intUbigeoDiligenciaIdEditInput.setValueExpression("itemValue", expressionFactory.createValueExpression(elContext, "#{intUbigeoDiligenciaId}", TablaMaestra.class));
        intUbigeoDiligenciaIdEditInput.setConverter(new TablaMaestraConverter());
        intUbigeoDiligenciaIdEditInput.setRequired(false);
        htmlPanelGrid.getChildren().add(intUbigeoDiligenciaIdEditInput);
        
        Message intUbigeoDiligenciaIdEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        intUbigeoDiligenciaIdEditInputMessage.setId("intUbigeoDiligenciaIdEditInputMessage");
        intUbigeoDiligenciaIdEditInputMessage.setFor("intUbigeoDiligenciaIdEditInput");
        intUbigeoDiligenciaIdEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(intUbigeoDiligenciaIdEditInputMessage);
        
        HtmlOutputText strNumeroExpedienteIdEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        strNumeroExpedienteIdEditOutput.setId("strNumeroExpedienteIdEditOutput");
        strNumeroExpedienteIdEditOutput.setValue("Str Numero Expediente Id:   ");
        htmlPanelGrid.getChildren().add(strNumeroExpedienteIdEditOutput);
        
        AutoComplete strNumeroExpedienteIdEditInput = (AutoComplete) application.createComponent(AutoComplete.COMPONENT_TYPE);
        strNumeroExpedienteIdEditInput.setId("strNumeroExpedienteIdEditInput");
        strNumeroExpedienteIdEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{diligenciaBean.diligencia.strNumeroExpedienteId}", Expediente.class));
        strNumeroExpedienteIdEditInput.setCompleteMethod(expressionFactory.createMethodExpression(elContext, "#{diligenciaBean.completeStrNumeroExpedienteId}", List.class, new Class[] { String.class }));
        strNumeroExpedienteIdEditInput.setDropdown(true);
        strNumeroExpedienteIdEditInput.setValueExpression("var", expressionFactory.createValueExpression(elContext, "strNumeroExpedienteId", String.class));
        strNumeroExpedienteIdEditInput.setValueExpression("itemLabel", expressionFactory.createValueExpression(elContext, "#{strNumeroExpedienteId.strLugarSuceso} #{strNumeroExpedienteId.datFechaRegistro} #{strNumeroExpedienteId.datFechaSuceso} #{strNumeroExpedienteId.strNumeroExpedienteId}", String.class));
        strNumeroExpedienteIdEditInput.setValueExpression("itemValue", expressionFactory.createValueExpression(elContext, "#{strNumeroExpedienteId}", Expediente.class));
        strNumeroExpedienteIdEditInput.setConverter(new ExpedienteConverter());
        strNumeroExpedienteIdEditInput.setRequired(false);
        htmlPanelGrid.getChildren().add(strNumeroExpedienteIdEditInput);
        
        Message strNumeroExpedienteIdEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        strNumeroExpedienteIdEditInputMessage.setId("strNumeroExpedienteIdEditInputMessage");
        strNumeroExpedienteIdEditInputMessage.setFor("strNumeroExpedienteIdEditInput");
        strNumeroExpedienteIdEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(strNumeroExpedienteIdEditInputMessage);
        
        HtmlOutputText datFechaInicioEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        datFechaInicioEditOutput.setId("datFechaInicioEditOutput");
        datFechaInicioEditOutput.setValue("Dat Fecha Inicio: * ");
        htmlPanelGrid.getChildren().add(datFechaInicioEditOutput);
        
        Calendar datFechaInicioEditInput = (Calendar) application.createComponent(Calendar.COMPONENT_TYPE);
        datFechaInicioEditInput.setId("datFechaInicioEditInput");
        datFechaInicioEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{diligenciaBean.diligencia.datFechaInicio}", Date.class));
        datFechaInicioEditInput.setNavigator(true);
        datFechaInicioEditInput.setEffect("slideDown");
        datFechaInicioEditInput.setPattern("dd/MM/yyyy");
        datFechaInicioEditInput.setRequired(true);
        htmlPanelGrid.getChildren().add(datFechaInicioEditInput);
        
        Message datFechaInicioEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        datFechaInicioEditInputMessage.setId("datFechaInicioEditInputMessage");
        datFechaInicioEditInputMessage.setFor("datFechaInicioEditInput");
        datFechaInicioEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(datFechaInicioEditInputMessage);
        
        HtmlOutputText datFechaFinEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        datFechaFinEditOutput.setId("datFechaFinEditOutput");
        datFechaFinEditOutput.setValue("Dat Fecha Fin: * ");
        htmlPanelGrid.getChildren().add(datFechaFinEditOutput);
        
        Calendar datFechaFinEditInput = (Calendar) application.createComponent(Calendar.COMPONENT_TYPE);
        datFechaFinEditInput.setId("datFechaFinEditInput");
        datFechaFinEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{diligenciaBean.diligencia.datFechaFin}", Date.class));
        datFechaFinEditInput.setNavigator(true);
        datFechaFinEditInput.setEffect("slideDown");
        datFechaFinEditInput.setPattern("dd/MM/yyyy");
        datFechaFinEditInput.setRequired(true);
        htmlPanelGrid.getChildren().add(datFechaFinEditInput);
        
        Message datFechaFinEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        datFechaFinEditInputMessage.setId("datFechaFinEditInputMessage");
        datFechaFinEditInputMessage.setFor("datFechaFinEditInput");
        datFechaFinEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(datFechaFinEditInputMessage);
        
        HtmlOutputText strObservacionEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        strObservacionEditOutput.setId("strObservacionEditOutput");
        strObservacionEditOutput.setValue("Str Observacion: * ");
        htmlPanelGrid.getChildren().add(strObservacionEditOutput);
        
        InputTextarea strObservacionEditInput = (InputTextarea) application.createComponent(InputTextarea.COMPONENT_TYPE);
        strObservacionEditInput.setId("strObservacionEditInput");
        strObservacionEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{diligenciaBean.diligencia.strObservacion}", String.class));
        LengthValidator strObservacionEditInputValidator = new LengthValidator();
        strObservacionEditInputValidator.setMaximum(200);
        strObservacionEditInput.addValidator(strObservacionEditInputValidator);
        strObservacionEditInput.setRequired(true);
        htmlPanelGrid.getChildren().add(strObservacionEditInput);
        
        Message strObservacionEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        strObservacionEditInputMessage.setId("strObservacionEditInputMessage");
        strObservacionEditInputMessage.setFor("strObservacionEditInput");
        strObservacionEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(strObservacionEditInputMessage);
        
        HtmlOutputText chrEstadoEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        chrEstadoEditOutput.setId("chrEstadoEditOutput");
        chrEstadoEditOutput.setValue("Chr Estado: * ");
        htmlPanelGrid.getChildren().add(chrEstadoEditOutput);
        
        InputText chrEstadoEditInput = (InputText) application.createComponent(InputText.COMPONENT_TYPE);
        chrEstadoEditInput.setId("chrEstadoEditInput");
        chrEstadoEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{diligenciaBean.diligencia.chrEstado}", Character.class));
        chrEstadoEditInput.setRequired(true);
        htmlPanelGrid.getChildren().add(chrEstadoEditInput);
        
        Message chrEstadoEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        chrEstadoEditInputMessage.setId("chrEstadoEditInputMessage");
        chrEstadoEditInputMessage.setFor("chrEstadoEditInput");
        chrEstadoEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(chrEstadoEditInputMessage);
        
        return htmlPanelGrid;
    }

	public HtmlPanelGrid populateViewPanel() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Application application = facesContext.getApplication();
        ExpressionFactory expressionFactory = application.getExpressionFactory();
        ELContext elContext = facesContext.getELContext();
        
        HtmlPanelGrid htmlPanelGrid = (HtmlPanelGrid) application.createComponent(HtmlPanelGrid.COMPONENT_TYPE);
        
        HtmlOutputText detalleDiligenciasLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        detalleDiligenciasLabel.setId("detalleDiligenciasLabel");
        detalleDiligenciasLabel.setValue("Detalle Diligencias:   ");
        htmlPanelGrid.getChildren().add(detalleDiligenciasLabel);
        
        HtmlOutputText detalleDiligenciasValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        detalleDiligenciasValue.setId("detalleDiligenciasValue");
        detalleDiligenciasValue.setValue("This relationship is managed from the DetalleDiligencia side");
        htmlPanelGrid.getChildren().add(detalleDiligenciasValue);
        
        HtmlOutputText intUbigeoDiligenciaIdLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        intUbigeoDiligenciaIdLabel.setId("intUbigeoDiligenciaIdLabel");
        intUbigeoDiligenciaIdLabel.setValue("Int Ubigeo Diligencia Id:   ");
        htmlPanelGrid.getChildren().add(intUbigeoDiligenciaIdLabel);
        
        HtmlOutputText intUbigeoDiligenciaIdValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        intUbigeoDiligenciaIdValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{diligenciaBean.diligencia.intUbigeoDiligenciaId}", TablaMaestra.class));
        intUbigeoDiligenciaIdValue.setConverter(new TablaMaestraConverter());
        htmlPanelGrid.getChildren().add(intUbigeoDiligenciaIdValue);
        
        HtmlOutputText strNumeroExpedienteIdLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        strNumeroExpedienteIdLabel.setId("strNumeroExpedienteIdLabel");
        strNumeroExpedienteIdLabel.setValue("Str Numero Expediente Id:   ");
        htmlPanelGrid.getChildren().add(strNumeroExpedienteIdLabel);
        
        HtmlOutputText strNumeroExpedienteIdValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        strNumeroExpedienteIdValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{diligenciaBean.diligencia.strNumeroExpedienteId}", Expediente.class));
        strNumeroExpedienteIdValue.setConverter(new ExpedienteConverter());
        htmlPanelGrid.getChildren().add(strNumeroExpedienteIdValue);
        
        HtmlOutputText datFechaInicioLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        datFechaInicioLabel.setId("datFechaInicioLabel");
        datFechaInicioLabel.setValue("Dat Fecha Inicio:   ");
        htmlPanelGrid.getChildren().add(datFechaInicioLabel);
        
        HtmlOutputText datFechaInicioValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        datFechaInicioValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{diligenciaBean.diligencia.datFechaInicio}", Date.class));
        DateTimeConverter datFechaInicioValueConverter = (DateTimeConverter) application.createConverter(DateTimeConverter.CONVERTER_ID);
        datFechaInicioValueConverter.setPattern("dd/MM/yyyy");
        datFechaInicioValue.setConverter(datFechaInicioValueConverter);
        htmlPanelGrid.getChildren().add(datFechaInicioValue);
        
        HtmlOutputText datFechaFinLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        datFechaFinLabel.setId("datFechaFinLabel");
        datFechaFinLabel.setValue("Dat Fecha Fin:   ");
        htmlPanelGrid.getChildren().add(datFechaFinLabel);
        
        HtmlOutputText datFechaFinValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        datFechaFinValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{diligenciaBean.diligencia.datFechaFin}", Date.class));
        DateTimeConverter datFechaFinValueConverter = (DateTimeConverter) application.createConverter(DateTimeConverter.CONVERTER_ID);
        datFechaFinValueConverter.setPattern("dd/MM/yyyy");
        datFechaFinValue.setConverter(datFechaFinValueConverter);
        htmlPanelGrid.getChildren().add(datFechaFinValue);
        
        HtmlOutputText strObservacionLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        strObservacionLabel.setId("strObservacionLabel");
        strObservacionLabel.setValue("Str Observacion:   ");
        htmlPanelGrid.getChildren().add(strObservacionLabel);
        
        InputTextarea strObservacionValue = (InputTextarea) application.createComponent(InputTextarea.COMPONENT_TYPE);
        strObservacionValue.setId("strObservacionValue");
        strObservacionValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{diligenciaBean.diligencia.strObservacion}", String.class));
        strObservacionValue.setReadonly(true);
        strObservacionValue.setDisabled(true);
        htmlPanelGrid.getChildren().add(strObservacionValue);
        
        HtmlOutputText chrEstadoLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        chrEstadoLabel.setId("chrEstadoLabel");
        chrEstadoLabel.setValue("Chr Estado:   ");
        htmlPanelGrid.getChildren().add(chrEstadoLabel);
        
        HtmlOutputText chrEstadoValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        chrEstadoValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{diligenciaBean.diligencia.chrEstado}", String.class));
        htmlPanelGrid.getChildren().add(chrEstadoValue);
        
        return htmlPanelGrid;
    }

	public Diligencia getDiligencia() {
        if (diligencia == null) {
            diligencia = new Diligencia();
        }
        return diligencia;
    }

	public void setDiligencia(Diligencia diligencia) {
        this.diligencia = diligencia;
    }

	public List<DetalleDiligencia> getSelectedDetalleDiligencias() {
        return selectedDetalleDiligencias;
    }

	public void setSelectedDetalleDiligencias(List<DetalleDiligencia> selectedDetalleDiligencias) {
        if (selectedDetalleDiligencias != null) {
            diligencia.setDetalleDiligencias(new HashSet<DetalleDiligencia>(selectedDetalleDiligencias));
        }
        this.selectedDetalleDiligencias = selectedDetalleDiligencias;
    }

	public List<TablaMaestra> completeIntUbigeoDiligenciaId(String query) {
        List<TablaMaestra> suggestions = new ArrayList<TablaMaestra>();
        for (TablaMaestra tablaMaestra : tablaMaestraService.findAllTablaMaestras()) {
            String tablaMaestraStr = String.valueOf(tablaMaestra.getStrDescripcion() +  " "  + tablaMaestra.getChrEstado() +  " "  + tablaMaestra.getIntTablaId());
            if (tablaMaestraStr.toLowerCase().startsWith(query.toLowerCase())) {
                suggestions.add(tablaMaestra);
            }
        }
        return suggestions;
    }

	public List<Expediente> completeStrNumeroExpedienteId(String query) {
        List<Expediente> suggestions = new ArrayList<Expediente>();
        for (Expediente expediente : expedienteService.findAllExpedientes()) {
            String expedienteStr = String.valueOf(expediente.getStrLugarSuceso() +  " "  + expediente.getDatFechaRegistro() +  " "  + expediente.getDatFechaSuceso() +  " "  + expediente.getStrNumeroExpedienteId());
            if (expedienteStr.toLowerCase().startsWith(query.toLowerCase())) {
                suggestions.add(expediente);
            }
        }
        return suggestions;
    }

	public String onEdit() {
        if (diligencia != null && diligencia.getDetalleDiligencias() != null) {
            selectedDetalleDiligencias = new ArrayList<DetalleDiligencia>(diligencia.getDetalleDiligencias());
        }
        return null;
    }

	public boolean isCreateDialogVisible() {
        return createDialogVisible;
    }

	public void setCreateDialogVisible(boolean createDialogVisible) {
        this.createDialogVisible = createDialogVisible;
    }

	public String displayList() {
        createDialogVisible = false;
        findAllDiligencias();
        return "diligencia";
    }

	public String displayCreateDialog() {
        diligencia = new Diligencia();
        createDialogVisible = true;
        return "diligencia";
    }

	public String persist() {
        String message = "";
        if (diligencia.getIntDiligenciaId() != null) {
            diligenciaService.updateDiligencia(diligencia);
            message = "Successfully updated";
        } else {
            diligenciaService.saveDiligencia(diligencia);
            message = "Successfully created";
        }
        RequestContext context = RequestContext.getCurrentInstance();
        context.execute("createDialog.hide()");
        context.execute("editDialog.hide()");
        
        FacesMessage facesMessage = new FacesMessage(message);
        FacesContext.getCurrentInstance().addMessage(null, facesMessage);
        reset();
        return findAllDiligencias();
    }

	public String delete() {
        diligenciaService.deleteDiligencia(diligencia);
        FacesMessage facesMessage = new FacesMessage("Successfully deleted");
        FacesContext.getCurrentInstance().addMessage(null, facesMessage);
        reset();
        return findAllDiligencias();
    }

	public void reset() {
        diligencia = null;
        selectedDetalleDiligencias = null;
        createDialogVisible = false;
    }

	public void handleDialogClose(CloseEvent event) {
        reset();
    }

	private static final long serialVersionUID = 1L;
}
